/**
 * Created by red on 2017/1/6.
 */
/*气泡框提示
* */
function popTip(msg,o,cstl) {
    if (o.type == 3) {
        $(".tip-err").remove();
        var div = $("<div class='tip-err'><div></div> <div ></div></div>");
        div.find(":first").text(msg);
        var offset = o.obj.offset();

        div.offset({
            top: offset.top - 40,
            left: offset.left
        });
        $(".tel").after(div);
        setTimeout(function () {
            div.remove();
        }, 3000)
    }
}

/**
 * 2017-04-11 更新
 * @param url 发送验证码地址
 * @param liveTime 有效时长
 * @param busType 业务类型 StaticValue
 * @param phoneNumberId 页面手机号 domId
 */
function sendVerifyCodeNew(url, liveTime, busType, phoneNumberId){
    var regex = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|^1[0-9]{10}$/;
    var phoneNumber = $("#" + phoneNumberId).val();
    if($.isEmptyObject(phoneNumber) || phoneNumber == ''){
        popTip("请输入手机号码",{type:3,obj:$("#" + phoneNumberId)});
        return;
    }
    if(!regex.test(phoneNumber)){
        popTip("号码格式不正确",{type:3,obj:$("#" + phoneNumberId)});
        return;
    }
    $.getJSON(url, {phoneNumber : phoneNumber,busType : busType}, function(data){
        if(data.success){
            $('#sendBtn').removeAttr('href');
            $('#sendBtn').css('background-color','#D7DFE8').css('border','1px solid #D7DFE8').html(liveTime);
            $("#" + phoneNumberId).attr('readonly','readonly');
            var time = setInterval(function(){
                var ss = Number($('#sendBtn').html());
                if(ss != 0){
                    $('#sendBtn').html(ss-1);
                }else{
                    window.clearInterval(time);
                    $('#sendBtn').css('background-color','#eb606b').css('border','1px solid #e70012').html("发送验证码");
                    $('#sendBtn').attr('href','javascript:sendVerifyCodeNew(\''+ url +'\','+liveTime+',\''+ busType +'\',\''+ phoneNumberId +'\')');
                    $("#" + phoneNumberId).removeAttr('readonly');
                    return;
                }
            },1000);
        }else{
            mui.alert(data.msg);
        }
    });
}

function sendVerifyCode(liveTime,obj){
    obj = $("#formobj").find("input[name='contactsPhone']");
    if($.isEmptyObject(obj)){
       return;
    }
    var phoneNumber = obj.val();
    var regex = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|^1[0-9]{10}$/;
    if($.isEmptyObject(phoneNumber) || phoneNumber == ''){
        popTip("请输入手机号码",{type:3,obj:obj});
        return;
    }
    if(!regex.test(phoneNumber)){
        popTip("号码格式不正确",{type:3,obj:obj});
        return;
    }
    $.getJSON('wxMenuController.do?sendVerifyCode',{phoneNumber:phoneNumber},function(data){
        if(data.success){
            $('#sendBtn').removeAttr('href');
            $('#sendBtn').css('background-color','#D7DFE8').css('border','1px solid #D7DFE8').html(liveTime);
            $("input[name='contactsPhone']").attr('disabled','disabled');
            var time = setInterval(function(){
                var ss = Number($('#sendBtn').html());
                if(ss != 0){
                    $('#sendBtn').html(ss-1);
                }else{
                    window.clearInterval(time);
                    $('#sendBtn').css('background-color','#eb606b').css('border','1px solid #e70012').html("验证码");
                    $('#sendBtn').attr('href','javascript:sendVerifyCode('+liveTime+')');
                    $("input[name='contactsPhone']").removeAttr('disabled');
                    return;
                }
            },1000);
        }else{
            mui.alert(data.msg);
        }
    });
}

function serviceSub(formValidName,submitUrl,serviceType){
    if (formValidName.check() == false) {
        $('#formobj').submit();
    } else {
        var phoneNum = $("#formobj").find("input[name='contactsPhone']").val();
        if(phoneNum == ''){
            return;
        }
        var redirectUrl = "wxMenuController.do?subSuc&type="+serviceType+"&phone="+phoneNum;
        mui.confirm("您确认提交订单？",'',['取消','确认'],function(idx){
            if(idx.index == 1){
                openMask();
                $.getJSON("wxMenuController.do?checkVerify",{verifyCode : $("#verifyCode").val()},function(re){
                    closeMask();
                    if(re.success){
                        $.post(submitUrl, $("#formobj").serialize()+"&contactsPhone="+phoneNum, function (data) {
                            checkServiceUser(phoneNum,$("#formobj").find("input[name='contactsName']").val());
                            data = $.parseJSON(data);
                            if (data.success) {
                                mui.openWindow({
                                    createNew: true,
                                    url: redirectUrl+"&param=" + data.obj
                                })
                            } else {
                                mui.alert(data.msg,function(){
                                    $(".mui-backdrop").remove();
                                });
                            }
                        });
                    }else{
                        mui.alert(re.msg,function(){
                            $(".mui-backdrop").remove();
                        });
                    }
                })
            }
        })
    }
}

function checkServiceUser(phonenum,contactName){
    $.getJSON("wxMenuController.do?serviceUser",{phoneNum:phonenum,contactName:contactName},function(data){
        
    })
}
var _service_default_mask = undefined;
function openMask() {
    mui("body").progressbar().show();
    if(_service_default_mask == undefined){
        _service_default_mask = mui.createMask(function(){
            return false;
        });
    }
    _service_default_mask.show();
}

function closeMask() {
    try{
        mui("body").progressbar().hide();
        _service_default_mask.close();
    }catch(e){

    }
}